package com.sbm.controller;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.sbm.cmd.TestCmd;
import com.sbm.datasource.dynamic.DynamicDataSourceSwitcher;
import com.sbm.datasource.dynamic.DynamicDataSourceTarget;
import com.sbm.domain.model.TestModel;
import com.sbm.svc.TestService;
import com.sbm.svc.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "测试API")
@RestController
@RequestMapping("/test")
public class TestController {
	@Autowired
	private UserService userService;

	@Autowired
	private TestService testService;

	@Autowired
	private DynamicDataSourceSwitcher switcher;

	@ApiOperation(value = "测试Index", notes = "查询110患者")
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	@RequiresRoles({"管理员"})
	public TestModel index() {
		TestCmd cmd = new TestCmd();
		cmd.setCardNo("0000000110");
		TestModel test = this.testService.selectPatient(cmd);
		return test;
	}

	@ApiOperation(value = "查询患者", notes = "查询用户")
	@ApiImplicitParam(name = "cmd", value = "查询命令", required = true, dataType = "TestCmd")
	@RequestMapping(value = "/selectPatient", method = RequestMethod.POST)
	public TestModel selectPatient(@RequestBody TestCmd cmd) {
		TestModel test = this.testService.selectPatient(cmd);
		return test;
	}

	@ApiOperation(value = "多参数测试", notes = "多参数")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "String"),
			@ApiImplicitParam(name = "name", value = "用户name", required = true, dataType = "String") })
	@RequestMapping(value = "/multiParam", method = RequestMethod.POST)
	public String multiParam(@PathVariable String id, @PathVariable String name) {
		return "ID:" + id + " Name:" + name;
	}

	@ApiOperation(value = "动态数据源Wjhis", notes = "切换到Wjhis")
	@DynamicDataSourceTarget("wjhis")
	@RequestMapping(value = "/dynamicWjhis", method = RequestMethod.POST)
	public TestModel dynamicWjhis() {
		TestCmd cmd = new TestCmd();
		cmd.setCardNo("0000000110");
		return this.testService.selectPatient(cmd);
	}

	@ApiOperation(value = "动态数据源默认", notes = "使用默认数据源")
	@RequestMapping(value = "/dynamicDb2test", method = RequestMethod.POST)
	public TestModel dynamicDb2test() {
		TestCmd cmd = new TestCmd();
		cmd.setCardNo("0000000110");
		return this.testService.selectPatient(cmd);
	}

	@ApiOperation(value = "动态数据源手动切换", notes = "手动切换数据源")
	@RequestMapping(value = "/dynamicSwitch", method = RequestMethod.POST)
	public String dynamicSwitch() {
		String result = "";
		TestCmd cmd = new TestCmd();
		cmd.setCardNo("0000000110");
		this.switcher.switchTo("wjhis");
		result += "WJHIS:" + JSON.toJSONString(this.testService.selectPatient(cmd)) + "\n";
		this.switcher.backFrom("wjhis");
		result += "DEFAULT:" + JSON.toJSONString(this.testService.selectPatient(cmd)) + "\n";
		return result;
	}
}
